package info.maxhis.util;

import info.maxhis.bn.R;
import info.maxhis.bn.birthday.BirthdayListActivity;
import info.maxhis.entity.Friend;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.widget.Toast;

/**
 * 用于共享数据的类，相当于存储全局变量
 * @author iStar
 *
 */
public class SharedData {
	private Context context;
	private Resources rs;
	
	public SharedData(Context _context){
		context=_context;
		rs = context.getResources();
	}
	
	
	/**
	 * 获得提前提醒的时间
	 * @return
	 */
	public int getAdvanceTime() {
		SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(context);
		String advanceStr = share.getString("advanceTime", "0");
		return Integer.parseInt(advanceStr);
	}
	
	/**
	 * 是否下载人人网头像
	 * @return
	 */
	public boolean downloadRenrenHead(){
		SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(context);
		return share.getBoolean("downloadHead", true);
	}
	
	/**
	 * 是否设置将好友信息添加到Google Calendar
	 * @return
	 */
	public boolean isSyncGoogleCalendar() {
		SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(context);
		return share.getBoolean("syncCalendar", true);
	}
	
	/**
	 * 保存Friend id
	 * @param id
	 */
	public void saveFriendId(String id) {
		SharedPreferences share = context.getSharedPreferences("friend", 0);
		share.edit().putString("fid", id).commit();
	}
	
	/**
	 * 得到已经保存的Friend Id
	 * @return
	 */
	public String getFriendId(){
		SharedPreferences share = context.getSharedPreferences("friend", 0);
		return share.getString("fid", "0");
	}
	
	/**
	 * 显示提醒对话框
	 * @param msg		提醒信息
	 */
	public void showAlertDialog(String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(msg).setCancelable(false)
				.setIcon(R.drawable.info).setTitle(rs.getString(R.string.notice))
				.setPositiveButton(rs.getString(R.string.confern), new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	/**
	 * 显示Toast
	 * @param resourseId
	 */
	public void showToast(int resourseId){
		Toast.makeText(context, rs.getString(resourseId), Toast.LENGTH_SHORT).show();
	}

	/**
	 * 将SMS id存入共享数据中
	 * @param sid
	 */
	public void saveSMSId(String sid) {
		SharedPreferences share = context.getSharedPreferences("friend", 0);
		share.edit().putString("sid", sid).commit();
	}
	
	/**
	 * 得到已经保存的SMS Id
	 * @return
	 */
	public String getSMSId(){
		SharedPreferences share = context.getSharedPreferences("friend", 0);
		return share.getString("sid", "0");
	}
	
	/**
	 * 当f生日到来时，产生提醒
	 * @param f
	 */
	public void makeNotification(Friend f){
		NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);;
		Intent it = new Intent(context, BirthdayListActivity.class);
		it.putExtra("name", f.getName());
		it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pIntent = PendingIntent.getActivity(context, 0, it, 0);
		Notification noti = new Notification();
		noti.icon=R.drawable.icon;
		noti.tickerText=f.getName()+rs.getString(R.string.birthday_coming);
		noti.defaults=Notification.DEFAULT_ALL;
		noti.setLatestEventInfo(context, rs.getString(R.string.app_name), noti.tickerText, pIntent);
		noti.flags = Notification.FLAG_AUTO_CANCEL;
		//振动
		noti.defaults|=Notification.DEFAULT_VIBRATE;
	    long[] vib={0,50,100,150,200};
	    noti.vibrate=vib;
	    //闪光灯
	    noti.defaults|=Notification.DEFAULT_LIGHTS;
	    noti.ledARGB|=0x99f0ff00;
	    noti.ledOnMS|=300;
	    noti.ledOffMS|=2000;
	    noti.flags |=Notification.FLAG_SHOW_LIGHTS;
		notiManager.notify(0, noti);
	}
	
	/**
	 * 保存人人网好友 ids（以“,”分割）
	 * @param ids
	 */
	public void saveRenrenIds(String ids) {
		SharedPreferences share = context.getSharedPreferences("friend", 0);
		share.edit().putString("ids", ids).commit();
	}
	
	/**
	 * 得到已经保存的人人网好友ids
	 * @return
	 */
	public String getRenrenIds(){
		SharedPreferences share = context.getSharedPreferences("friend", 0);
		return share.getString("ids", null);
	}
	/**
	* 得到本应用的apikey
	* @return
	*/
	public String getApiKey(){
		return "28c6bb5fb2dc461db22ba917815ef8a7";
	}
}
